{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "Untitled1.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jHtagUUT3LYZ",
        "outputId": "b6fed23b-5933-4df8-a6af-0d7e242a1b1a"
      },
      "source": [
        "!git clone https://github.com/Sharpiless/Yolov5-distillation-train-inference.git"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'Yolov5-distillation-train-inference'...\n",
            "remote: Enumerating objects: 158, done.\u001b[K\n",
            "remote: Counting objects: 100% (158/158), done.\u001b[K\n",
            "remote: Compressing objects: 100% (111/111), done.\u001b[K\n",
            "remote: Total 158 (delta 74), reused 129 (delta 45), pack-reused 0\u001b[K\n",
            "Receiving objects: 100% (158/158), 1.01 MiB | 28.70 MiB/s, done.\n",
            "Resolving deltas: 100% (74/74), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k37VtAyE4I86",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "cd3bcd11-c43d-4918-ae74-116c3c7c5eb5"
      },
      "source": [
        "%cd Yolov5-distillation-train-inference/"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content/Yolov5-distillation-train-inference\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4NgA6oZi4RzR"
      },
      "source": [
        "!pip install -r requirements.txt"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0lTGwFevbYuw",
        "outputId": "96385a3f-9c10-4c81-f62e-844a9fc52bcd"
      },
      "source": [
        "!nvidia-smi"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Tue May 11 01:29:22 2021       \n",
            "+-----------------------------------------------------------------------------+\n",
            "| NVIDIA-SMI 465.19.01    Driver Version: 460.32.03    CUDA Version: 11.2     |\n",
            "|-------------------------------+----------------------+----------------------+\n",
            "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
            "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
            "|                               |                      |               MIG M. |\n",
            "|===============================+======================+======================|\n",
            "|   0  Tesla T4            Off  | 00000000:00:04.0 Off |                    0 |\n",
            "| N/A   63C    P8    10W /  70W |      0MiB / 15109MiB |      0%      Default |\n",
            "|                               |                      |                  N/A |\n",
            "+-------------------------------+----------------------+----------------------+\n",
            "                                                                               \n",
            "+-----------------------------------------------------------------------------+\n",
            "| Processes:                                                                  |\n",
            "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
            "|        ID   ID                                                   Usage      |\n",
            "|=============================================================================|\n",
            "|  No running processes found                                                 |\n",
            "+-----------------------------------------------------------------------------+\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kVRCcvcP8373"
      },
      "source": [
        "!cp data/scripts/get_voc.sh ./"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_eJrrDUv9JVF",
        "outputId": "d83de7c4-2b3d-4265-e99e-c997f4675e31"
      },
      "source": [
        "!bash get_voc.sh"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading https://github.com/ultralytics/yolov5/releases/download/v1.0/VOCtest_06-Nov-2007.zip ...\n",
            "Downloading https://github.com/ultralytics/yolov5/releases/download/v1.0/VOCtrainval_06-Nov-2007.zip ...\n",
            "   % %  TotalT    o% Rteceivaed %l Xfe rd   Avera ge S % peed R  Tieceme  i  Tivme   e  Tidme  C % urrenXt\n",
            "   f    e     r    d      A     v  e     r Dloagead  U ploaSd   pTotael   eSpendt     Lef  t  SpTeed\n",
            "i\r  0m   e  0      0      0     T0   im  0  e     0        0 --  :--:-T- --i:--:m-- e--:-- :--     0 Current\n",
            "                                 Dload  Upload   Total   Spent    Left  Speed\n",
            "\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   635  100   635    0     0   6165      0 --:--:-- --:--:-- --:--:--  6165\n",
            "100   631  100   631    0     0   5788      0 --:--:-- --:--:-- --:--:--  5842\n",
            "100  425M  100  425M    0     0  62.9M      0  0:00:06  0:00:06 --:--:-- 74.0M\n",
            "100  418M  100  418M    0     0  48.5M      0  0:00:08  0:00:08 --:--:-- 49.9M\n",
            "Completed in 15 seconds\n",
            "Splitting dataset...\n",
            "True\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P4A_vs06FkyG"
      },
      "source": [
        "!cp ../drive/MyDrive/yolov5l.pt ./weights"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uJM503irDN2G"
      },
      "source": [
        "!mkdir runs\n",
        "!mv ../drive/MyDrive/runs/* ./runs"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KdxZy_He9mBI",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1e20eca5-f19b-498c-bc85-9d34093c7643"
      },
      "source": [
        "!python train_distill.py --soft-loss --KL-loss --full-output-loss --with-gt-loss"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "\u001b[34m\u001b[1mautoanchor: \u001b[0mAnalyzing anchors... anchors/target = 4.10, Best Possible Recall (BPR) = 0.9999\n",
            "Image sizes 640 train, 640 test\n",
            "Using 0 dataloader workers\n",
            "Logging results to runs/train/exp\n",
            "Starting training for 30 epochs...\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "\r  0% 0/1253 [00:00<?, ?it/s]/content/Yolov5-distillation-train-inference/teacher.py:97: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
            "  xywh = (xyxy2xywh(torch.tensor(xyxy.cpu()).view(1, 4)\n",
            "      0/29     1.03G   0.06152   0.03453   0.05578    0.1788   0.02694        23       640: 100% 1253/1253 [05:31<00:00,  3.78it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:20<00:00,  7.68it/s]\n",
            "                 all        4952       12032         0.2       0.468        0.24      0.0869\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      1/29     2.48G     0.054   0.02672    0.0253    0.1179   0.01185        10       640: 100% 1253/1253 [05:18<00:00,  3.94it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.23it/s]\n",
            "                 all        4952       12032       0.366       0.476       0.275      0.0912\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      2/29     2.48G   0.05237   0.03137   0.02901     0.124   0.01128         5       640: 100% 1253/1253 [05:19<00:00,  3.92it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.16it/s]\n",
            "                 all        4952       12032       0.308       0.276        0.11      0.0317\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      3/29     2.48G   0.05052   0.03509   0.03274      0.13   0.01165        13       640: 100% 1253/1253 [05:17<00:00,  3.94it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.19it/s]\n",
            "                 all        4952       12032       0.298       0.343       0.138      0.0393\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      4/29     2.48G   0.04732   0.03447   0.02936    0.1213   0.01016         6       640: 100% 1253/1253 [05:17<00:00,  3.94it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.17it/s]\n",
            "                 all        4952       12032       0.162        0.46       0.137      0.0383\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      5/29     2.48G   0.04354   0.03319   0.02673    0.1125  0.009019        17       640: 100% 1253/1253 [05:16<00:00,  3.96it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.16it/s]\n",
            "                 all        4952       12032       0.374       0.317       0.176      0.0552\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      6/29     2.48G   0.04199   0.03259   0.02487    0.1077  0.008263        14       640: 100% 1253/1253 [05:18<00:00,  3.93it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.19it/s]\n",
            "                 all        4952       12032       0.141       0.528       0.165       0.053\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      7/29     2.48G   0.04005   0.03174   0.02163    0.1007  0.007284        13       640: 100% 1253/1253 [05:20<00:00,  3.91it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:07<00:00,  9.20it/s]\n",
            "                 all        4952       12032       0.402       0.364       0.226      0.0797\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      8/29     2.48G   0.03864   0.03102   0.02056   0.09704  0.006826         7       640: 100% 1253/1253 [05:18<00:00,  3.94it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:06<00:00,  9.25it/s]\n",
            "                 all        4952       12032        0.41       0.346       0.224      0.0785\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "      9/29     2.48G   0.03737   0.03082    0.0199   0.09466  0.006558         9       640: 100% 1253/1253 [05:15<00:00,  3.97it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:06<00:00,  9.25it/s]\n",
            "                 all        4952       12032       0.415       0.347       0.236      0.0835\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "     10/29     2.48G   0.03676   0.02984   0.01818    0.0907  0.005925         7       640: 100% 1253/1253 [05:16<00:00,  3.96it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:06<00:00,  9.28it/s]\n",
            "                 all        4952       12032       0.341       0.413       0.213      0.0773\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "     11/29     2.48G   0.03567   0.02982   0.01738   0.08855  0.005686        17       640: 100% 1253/1253 [05:16<00:00,  3.96it/s]\n",
            "               Class      Images      Labels           P           R      mAP@.5  mAP@.5:.95: 100% 619/619 [01:06<00:00,  9.29it/s]\n",
            "                 all        4952       12032       0.381       0.405        0.24       0.089\n",
            "\n",
            "     Epoch   gpu_mem       box       obj       cls   distill     totalteacher-preds  img_size\n",
            "     12/29     2.48G   0.03388   0.03019   0.01506    0.0842  0.005071        23       640:  16% 202/1253 [00:51<04:35,  3.81it/s]Traceback (most recent call last):\n",
            "  File \"train_distill.py\", line 628, in <module>\n",
            "    train(hyp, opt, device, tb_writer)\n",
            "  File \"train_distill.py\", line 341, in train\n",
            "    pred = model(imgs)  # forward\n",
            "  File \"/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\", line 889, in _call_impl\n",
            "    result = self.forward(*input, **kwargs)\n",
            "  File \"/content/Yolov5-distillation-train-inference/models/yolo.py\", line 117, in forward\n",
            "    return self.forward_once(x, profile)  # single-scale inference, train\n",
            "  File \"/content/Yolov5-distillation-train-inference/models/yolo.py\", line 148, in forward_once\n",
            "    x = m(x)  # run\n",
            "  File \"/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\", line 889, in _call_impl\n",
            "    result = self.forward(*input, **kwargs)\n",
            "  File \"/content/Yolov5-distillation-train-inference/models/common.py\", line 42, in forward\n",
            "    return self.act(self.bn(self.conv(x)))\n",
            "  File \"/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\", line 889, in _call_impl\n",
            "    result = self.forward(*input, **kwargs)\n",
            "  File \"/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py\", line 399, in forward\n",
            "    return self._conv_forward(input, self.weight, self.bias)\n",
            "  File \"/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py\", line 396, in _conv_forward\n",
            "    self.padding, self.dilation, self.groups)\n",
            "KeyboardInterrupt\n",
            "     12/29     2.48G   0.03388   0.03019   0.01506    0.0842  0.005071        23       640:  16% 202/1253 [00:51<04:28,  3.92it/s]\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}